package com.sanjiahantang.youjiarole.module.common.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.bannerview.BannerView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.common.beans.ShopBean;
import com.sanjiahantang.youjiarole.module.common.holders.ShopHolder;
import com.sanjiahantang.youjiarole.module.common.views.impl.MainActivity;
import com.sanjiahantang.youjiarole.module.common.views.impl.WebActivity;
import com.sanjiahantang.youjiarole.module.match.beans.MatchListBean;
import com.sanjiahantang.youjiarole.utils.FileUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zy on 2017/8/9.
 */

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    //布局标识集合
    private LayoutInflater inflater;

    private ShopBean data;

    private final int TYPE_SIZE = 5;
    //各种类型
    public static final int TYPE_BANNER = 1;//轮播图
    public static final int TYPE_STYLE = 2;//风格列表
    public static final int TYPE_COLLOCATION_TITLE = 3;//搭配标题
    public static final int TYPE_COLLOCATION = 4;//搭配
    public static final int TYPE_RECOMMEND = 5;//家具推荐

    private static int matchCounter = 0;
    private static int recommendCounter = 0;
    private List<Integer> typeList;


    public ShopAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);


    }

    public ShopAdapter(Context context, ShopBean data) {
        this(context);
        this.data = data;

        typeList = new ArrayList<>();
        typeList.add(1);
        typeList.add(2);
        typeList.add(3);
        for (int i = 0,max = data.getTogeterList().size();i<max;i++){
            typeList.add(4);
        }
        for (int i = 0,max = data.getCategoryFullList().size();i<max;i++){
            typeList.add(5);
        }
    }

    @Override
    public int getItemViewType(int position) {

        return typeList.get(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case TYPE_BANNER:   //轮播图和输入框
                BannerView bv = new BannerView(context);
                bv.setHeight(200);
                bv.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, Utils.dip2px(context, 200)));

                List<HashMap<String,Object>> imgList = new ArrayList<>();
                for (final ShopBean.Advisement url:data.getGoodsPageAdList()) {
                    HashMap<String,Object> map = new HashMap<>();
                    map.put("img", FileUtils.rePath(url.getUrl()));
                    map.put("action", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra("url",url.getLinkLocation());
                        }
                    });
                    imgList.add(map);
                }
                bv.initAdapterData(imgList);
                bv.setDrawerMode(BannerView.STYLE_Dot_Rect);
                bv.setLocationMode(BannerView.MODE_RIGHT);

                return new ShopHolder((Activity) context,bv,TYPE_BANNER,null);
//                return new BannerHolder(inflater.inflate(R.layout.rv_item_banner, null),
//                        context, data.getAds());
            case TYPE_STYLE: //功能菜单
                return new ShopHolder((Activity) context,inflater.inflate(R.layout.item_horizontal, null),TYPE_STYLE,data.getCategoryList());
            case TYPE_COLLOCATION_TITLE:    //搭配
                return new ShopHolder((Activity) context,inflater.inflate(R.layout.item_match_title, null),
                        TYPE_COLLOCATION_TITLE,data.getTogeterList());
            case TYPE_COLLOCATION:    //搭配
                MatchListBean.Detail match = null;
                if(matchCounter < data.getTogeterList().size()){
                    match = data.getTogeterList().get(matchCounter);
                    matchCounter++;
                }else{
                    matchCounter = 0;
                    match = data.getTogeterList().get(matchCounter);
                    matchCounter++;
                }
                return new ShopHolder((Activity) context,inflater.inflate(R.layout.item_match, null),
                        TYPE_COLLOCATION,match);
            case TYPE_RECOMMEND://家具推荐
                ShopBean.Category category = null;
                if(recommendCounter < data.getCategoryFullList().size()){
                    category = data.getCategoryFullList().get(recommendCounter);
                    recommendCounter++;
                }else{
                    recommendCounter = 0;
                    category = data.getCategoryFullList().get(recommendCounter);
                    recommendCounter++;
                }
                return new ShopHolder((Activity) context,inflater.inflate(R.layout.item_holder_collocation_remmend, null),
                        TYPE_RECOMMEND,category);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }


}
